﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySql.Data;
using MySql.Data.MySqlClient;
using System.Data;

namespace Game.Lib
{
    class Database
    {
        private MySqlConnection conn;
        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="host">主机</param>
        /// <param name="port">端口</param>
        /// <param name="uid">用户名</param>
        /// <param name="pwd">密码</param>
        /// <param name="name">数据库名称</param>
        public Database(string host,int port,string uid,string pwd,string name)
        {
            try
            {
                string connStr = String.Format("Database={0};Port={1};Server={2};User Id='{3}';Password='{4}';charset='utf8';pooling=true;",name,port,host,uid,pwd);
                this.conn = new MySqlConnection(connStr);
                //打开连接
                conn.Open();
                //防止乱码
                this.ExecuteNonQuery("SET NAMES 'utf8'");
            }
            catch (MySqlException err)
            {
                //发生异常，关闭数据库连接
                this.conn.Close();
                throw (new ApplicationException(err.Message));
            }
        }

        /// <summary>
        /// 执行不返回结果集的查询
        /// </summary>
        /// <param name="cmdText">查询SQL</param>
        /// <returns>影响行数</returns>
        public int ExecuteNonQuery(string cmdText)
        {
            try
            {
                MySqlCommand cmd = new MySqlCommand(cmdText,this.conn);
                int val = cmd.ExecuteNonQuery();
                return val;
            }
            catch (MySqlException err)
            {
                //发生异常，关闭数据库连接
                this.conn.Close();
                throw (new ApplicationException(err.Message));
            }
        }

        /// <summary>
        /// 返回数据集
        /// </summary>
        /// <param name="cmdText">查询SQL</param>
        /// <returns>数据集</returns>
        public DataSet GetDataSet(string cmdText)
        {
            try
            {
                MySqlCommand cmd = new MySqlCommand(cmdText, this.conn);
                MySqlDataAdapter adapter = new MySqlDataAdapter();
                adapter.SelectCommand = cmd;
                DataSet ds = new DataSet();
                adapter.Fill(ds);
                return ds;
            }
            catch (MySqlException err)
            {
                //发生异常，关闭数据库连接
                this.conn.Close();
                throw (new ApplicationException(err.Message));
            }
        }

        /// <summary>
        /// 返回数据集的第一行
        /// </summary>
        /// <param name="cmdText">查询SQL</param>
        /// <returns>数据集</returns>
        public object ExecuteScalar(string cmdText)
        {
            try
            {
                MySqlCommand cmd = new MySqlCommand(cmdText, this.conn);
                object obj = cmd.ExecuteScalar();
                return obj;
            }
            catch (MySqlException err)
            {
                //发生异常，关闭数据库连接
                this.conn.Close();
                throw (new ApplicationException(err.Message));
            }
        }
        /// <summary>
        /// 关闭连接
        /// </summary>
        ~Database()
        {
            this.conn.Close();
        }
    }
}
